import 'dart:math';

import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/domain/widgets/ps_code_checking.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_easy_loading.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class DialogVertifivationCode extends StatefulWidget {
  final Function(bool isSuccess) vertifivationBlock;
  DialogVertifivationCode({required this.vertifivationBlock});
  @override
  DialogVertifivationCodeState createState() => DialogVertifivationCodeState();
}

class DialogVertifivationCodeState extends State<DialogVertifivationCode> {
  String code = '';
  bool showError = false;
  TextEditingController _controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
            margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 200.w),
            padding: EdgeInsets.only(
                top: 5.w, left: 20.w, bottom: 28.w, right: 20.w),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12), color: Colors.white),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      // color: Colors.red,
                      constraints: BoxConstraints(maxWidth: 250.w),
                      child: AutoSizeText(
                        S().captch_check,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: ClickUtils.debounce(() {
                          Navigator.pop(context);
                        }),
                        child: Container(
                          width: 40,
                          height: 40,
                          alignment: Alignment.centerRight,
                          child: Image.asset(
                              'assets/images/service_goods_close.png',
                              width: 15,
                              height: 15),
                        ))
                  ],
                ),

                Container(
                  margin: EdgeInsets.only(top: 20),
                  height: 52.w,
                  // padding: EdgeInsets.only(bottom: 20),
                  decoration: BoxDecoration(
                    // border: Border.all(
                    //   width: .5,
                    //   color: ThemeColor.colorBlack20,
                    // ),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    children: [
                      Expanded(
                          child: TextField(
                              controller: _controller,
                              onChanged: (value) {
                                if (value.length == 4) {
                                  if (value == code) {
                                    widget.vertifivationBlock(true);
                                    Navigator.pop(context);
                                  } else {
                                    _controller.text = '';
                                    refrushCode();
                                    if (mounted) {
                                      setState(() {
                                        // showError = true;
                                      });
                                    }
                                    customShowToast(S().incorrect_code);
                                  }
                                }
                              },
                              inputFormatters: [
                                FilteringTextInputFormatter.deny(
                                    appContext.emojiRegexp),
                                LengthLimitingTextInputFormatter(4)
                              ],
                              keyboardType: TextInputType.number,
                              decoration: InputDecoration(
                                hintText: S().captch_input,
                                contentPadding:
                                    EdgeInsets.symmetric(vertical: 0),
                                isCollapsed: true,
                                hintStyle: TextStyle(
                                    color: ThemeColor.colorBlack20,
                                    fontSize: 16),
                                border: OutlineInputBorder(
                                    borderSide: BorderSide.none),
                              ))),
                    ],
                  ),
                ),
                Container(
                  // margin: EdgeInsets.only(right: 5),
                  color: ThemeColor.colorBlack20,
                  height: .5,
                  // height: 28,
                ),
                SizedBox(
                  height: 15.w,
                ),
                SizedBox(
                  width: 140.w,
                  height: 50.w,
                  child: Stack(
                    children: [
                      Positioned.fill(
                          child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [CustomVertificationCode(code: code)],
                      )),
                      Positioned.fill(
                          child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: ClickUtils.debounce(() {
                          if (mounted) {
                            refrushCode();
                            setState(() {
                              showError = false;
                            });
                          }
                        }),
                        child: Visibility(
                            visible: showError,
                            child: Container(
                              color: ThemeColor.colorBlack50.withOpacity(0.2),
                              child: Icon(
                                Icons.refresh,
                                size: 24,
                                color: Colors.white,
                              ),
                            )),
                      ))
                    ],
                  ),
                ),
                // DytAliyunWidget(
                //   status: status,
                //   onSuccess: (data) {
                //     DLog(data);
                //     onSuccess(data);
                //   },
                // ),
              ],
            )),
        Expanded(child: SizedBox())
      ],
    );
  }

  refrushCode() {
    code = '';
    for (var i = 0; i < 4; i++) {
      code += Random().nextInt(9).toString();
    }
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void initState() {
    super.initState();
    refrushCode();
  }

  @override
  void dispose() {
    super.dispose();
    FocusScope.of(context).requestFocus(FocusNode());
    _controller.dispose();
  }

  @override
  void didUpdateWidget(DialogVertifivationCode oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
